/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.manager;

import MeteoCal.entity.Event;
import MeteoCal.entity.Weatherforecast;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Guttadoro, Iommazzo, Saullo
 */

@Stateless
public class WeatherManager {

    @PersistenceContext
    private EntityManager em;
    
    public List<Weatherforecast> findAll(){
        return em.createNamedQuery(Weatherforecast.findAll, Weatherforecast.class).getResultList();
    }
    
    public void createWeather(Event event){ // per gutta quando crea l0 ' event0
        Weatherforecast weatherforecast= new Weatherforecast();
        weatherforecast.setCity(event.getCity());
        weatherforecast.setDate(event.getDateBegin());
        weatherforecast.setDescription("Empty");
        weatherforecast.setResult_description("Empty");
        weatherforecast.setResult_icon("Empty");
        weatherforecast.setResult_country("Empty");
        em.persist(weatherforecast);
    }
    
    
    public void updateWeather(Weatherforecast f, Weatherforecast forecast){ 
        f.setDescription(forecast.getDescription());
        if(forecast.getDescription() != null)
            f.setResult_description(forecast.getResult_description());
        f.setResult_icon(forecast.getResult_icon());
        f.setResult_country(forecast.getResult_country());
        em.merge(f);
    }
    
    public void deleteWeather(Weatherforecast forecast){
        em.remove(forecast);
    }
     
    
     
    public List<Event> findByCityAndDate(String city, Date date){
        TypedQuery<Event> query = em.createNamedQuery(Event.findByCityAndDateBegin, Event.class);
        query.setParameter("city", city);
        query.setParameter("dateBegin", date);
        List<Event> events = query.getResultList();
        return events;
    }
     
  
    public List<Weatherforecast> findWeatherByCity(String city){
        TypedQuery<Weatherforecast> query = em.createNamedQuery(Weatherforecast.findByCity, Weatherforecast.class);
        query.setParameter("city", city);
        List<Weatherforecast> Weatherforecast = query.getResultList();
        
        return Weatherforecast;
    }
    
    public List<Weatherforecast> findWeatherByCityAndDate(String city, Date date){
        try {
            TypedQuery<Weatherforecast> query = em.createNamedQuery(Weatherforecast.findByCityAndDate, Weatherforecast.class);
            query.setParameter("city", city);
            query.setParameter("date", date);
            List<Weatherforecast> Weatherforecast = query.getResultList();
            return Weatherforecast;

        }catch(NoResultException e) {
            return null;
        }
    }
}
